<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>首页</title>
<jsp:include page="inc.jsp"></jsp:include>
<script type="text/javascript">
Ext.Loader.setConfig({
    enabled: true
});
Ext.Loader.setPath('App', 'app');
//Ext.require('App.ux.*');

var isLeftTree = false;
var treePanel;
var form, win;
var sysConfig;
function getSysConfig() {
    if (sysConfig == null) {
        Ext.Ajax.request({
            url: "commonController/getsystemconfig.action",
            scope: this,
            async: false,
            callback: function (o, s, r) {
                var resp = Ext.decode(r.responseText);
                sysConfig = {
                    productName: resp.productName,
                    vendor: resp.vendor,
                    copyright: resp.copyright,
                    version: resp.version,
                    website: resp.website,
                    buildId: resp.buildId,
                    userName: resp.userName
                }
            }
        });
    }
}
function createForm() {
    Ext.define('defaultForm', {
        extend: 'Ext.form.Panel',
        fields: new Ext.util.MixedCollection(),
        initComponent: function () {
            var me = this;
            var tn = 1;

            this.fields.add("password", new Ext.form.TextField({
                name: "password",
                inputType: "password",
                dataIndex: "password",
                fieldLabel: "原密码",
                allowBlank: false
                //错误信息的显示
            }));
            this.fields.add("newPwd", new Ext.form.TextField({
                name: "newPwd",
                inputType: "password",
                dataIndex: "newPwd",
                fieldLabel: "新密码",
                allowBlank: false
            }));
            this.fields.add("newPwdTwo", new Ext.form.TextField({
                name: "newPwdTwo",
                inputType: "password",
                dataIndex: "newPwdTwo",
                fieldLabel: "再次输入新密码",
                allowBlank: false
            }));
            this.items = [
                {
                    layout: "form",
                    columnWidth: 1,
                    labelAlign: "right",
                    labelWidth: 100,
                    items: [ this.fields.get("password"),
                        this.fields.get("newPwd"), this.fields.get("newPwdTwo")]
                }
            ];
            this.frame = true;
            this.layout = 'fit';
            this.region = 'east';
            this.border = true;
            this.split = true;
            this.callParent(arguments);
        },
        onRender: function () {
            this.callParent(arguments);
        },
        newDataRecord: function () {
            this.fields.get("password").setValue('');
            this.fields.get("newPwd").setValue('');
            this.fields.get("newPwdTwo").setValue('');
            createWindows();
        }
    });
}
function createWindows() {
    if (win == null) {
        win = new Ext.Window({
            title: '修改密码',
            iconCls: 'icon-win',
            autoHeight: true,
            width: 335,
            height: 400,
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: 'userController/savePwd.action',
                                scope: form,
                                success: function (form, action) {
                                    win.hide();
                                    grid.getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    },
                    id: "btnSave"
                },
                {
                    text: "关闭",
                    id: "btnClose",
                    handler: function () {
                        win.hide();
                    }
                }
            ],
            layout: 'fit',
            plain: false,
            closable: false,
            modal: true,
            items: [ form ]
        });
    }
    win.show();
}

//注销
function doLogout() {
    Ext.Msg.show({
        title: '注销确认',
        msg: '真的要注销吗?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: 'userController/logout.action',
                    method: "POST",
                    callback: function (o, s, r) {
                        var respText = Ext.JSON.decode(r.responseText);
                        if (respText.success) {

                            window.top.location.href = 'login.jsp';
                        } else {
                            if (!Ext.isEmpty(respText.message)) {
                                Ext.Msg.alert('错误', respText.message);
                            } else {
                                Ext.Msg.alert('错误', '因为不能取得服务端信息，不能正常注销。');
                            }
                        }
                    }
                });
            }
        },
        icon: Ext.MessageBox.QUESTION
    });
}
var contentPanel;
Ext.onReady(function () {
	
    Ext.QuickTips.init();
    getSysConfig();
     contentPanel = new Ext.TabPanel(
            {
                region: 'center',
                id: 'contentPanel',
                name: 'contentPanel',
                enableTabScroll: true,
                //实现标签滚动
                activeTab: 0,
                resizeTabs: true, // turn on tab resizing
                listeners: {
                    'itemcontextmenu': function (view, record, item, index, e, eOpts) {
                        alert('dd');
                    }
                },
                items: [
                    {
                        id: 'homePage',
                        title: '首页',
                        autoScroll: true,
                        html: '<div style="position:absolute;color:#ff0000;top:40%;left:40%;">Tree控件和TabPanel控件结合功能演示</div>'
                    }
                ]
            });
    Ext.define('treeModel', {
        extend: 'Ext.data.Model',
        fields: [
            {
                name: 'id',
                type: 'string'
            },
            {
                name: 'text',
                type: 'string'
            },
            {
                name: 'url',
                type: 'string'
            },
            {
                name: 'iconcls',
                type: 'string'
            }
        ]
    });

    var leftPanel = Ext.create('Ext.Panel', {
        id: 'leftPanel',
        region: 'west',
        layout: 'accordion',
        title: '导航栏',
        width: 200,
        minSize: 200,
        maxSize: 350,
        activeTab: 0,
        //items : [ treePanel ],
        collapsible: true
    });

    var store = new Ext.data.TreeStore({
        //fields:['text'],
        model: 'treeModel',
        autoLoad: false,
        expanded: false,
        proxy: {
            type: 'ajax',
            actionMethods: 'post',
            url: 'resourceController/getMenu.action',
            reader: {
                type: 'json'
            }
        },
        folderSort: true,
        sorters: [
            {
                property: 'text',
                direction: 'ASC'
            }
        ]
    });
    store.load();
    treePanel = new Ext.tree.TreePanel(
            {
                useArrows: true,
                autoScroll: true,
                layout: 'fit',
                animate: true,
                rootVisible: false,//是否显示根节点
                store: store,
                listeners: {
                    itemclick: function (v, r, item) {
                        var url = r.raw.url;
                        if (url != "" && url != 'null') {
                            url = '${pageContext.request.contextPath}/'
                                    + url;
                            var n = contentPanel.getComponent(r.raw.id);
                            if (contentPanel.items.length > 8) {
                                contentPanel.remove(1);
                            }
                            if (!n) { //判断是否已经打开该面板
                                n = contentPanel
                                        .add({ //新增tab
                                            id: r.data.id,
                                            title: r.data.text,
                                            closable: true,
                                            iconCls: r.raw.iconCls,
                                            destroy: function () {//销毁tabpanel 销毁tab在浏览器中的内存
                                                if (this.fireEvent("destroy", this) != false) {
                                                  //  this.el.remove();
                                                    if (Ext.isIE) {
                                                        CollectGarbage(); //CollectGarbage()函数强制收回内存
                                                    }
                                                }
                                            },
                                            layout : 'fit',
                                            items:[Ext.create('App.service.'+r.raw.url)]
                                        });
                            }
                            contentPanel.setActiveTab(n); //设置选择的tab为活动面板
                        }
                    }
                }
            });
    leftPanel.layout = 'fit';
    leftPanel.items.add(treePanel);

    var manView = new Ext.Viewport({
        renderTo: Ext.getBody(),
        layout: 'border',
        items: [
            {
                region: 'north', //子元素的方位：north、west、east、center、south
                xtype: "panel",
                height: 70,
                margins: '0 0 0 0',
                items: [
                    {
                        region: 'north',
                        html: '<div id="titlebar"  style="vertical-align:middle;height: 70px;background-image: url(plug-in/images/login.png);background-repeat: repeat-x;" >'
                                + '<div style="float:right;padding-top:25px;"><a href="#"><img src="plug-in/images/about.png" onclick="about()" height="32"/></a>'
                                + '<a href="#"><img src="plug-in/images/newPassword.png" onclick="createWindows()" height="32"/></a>'
                                + '<a href="#"><img src="plug-in/images/logout.png" onclick="doLogout()" height="32"/></a></div></div>',
                        border: false
                    }
                ]
            },
            leftPanel,
            contentPanel
            ,
            {
                region: 'south',
                xtype: "panel",
                html: "<div style='text-align:center'>" + sysConfig.productName + "</div>",
                height: 25
            }
        ]
    });
    manView.doLayout();
    createForm();
    form = Ext.create('defaultForm');
//    contentPanel
//            .add({ //新增tab
//                id: '1',
//                title: '2',
//                closable: true,
//                layout : 'fit',
//                items:[Ext.create('App.test')]
//            });
    
});
</script>
</head>
<body>


</body>
</html>